package com.sec.android.app.myfiles.feature.Hover;

import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.facade.MyFilesFacade;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.module.abstraction.AbsFileListAdapterImp;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.local.category.CategoryDbTableInfo;
import com.sec.android.app.myfiles.module.local.file.LocalHoverFileListAdapterImp;
import com.sec.android.app.myfiles.navigation.NavigationInfo;
import com.sec.android.app.myfiles.navigation.NavigationInfo.NavigationMode;
import com.sec.android.app.myfiles.util.FileUtils;
import com.sec.android.app.myfiles.util.PreferenceUtils;
import com.sec.android.app.myfiles.util.UiUtils;

import java.util.ArrayList;

public class FolderHoverView extends AbsHoverView {
    AbsFileListAdapterImp mAdapterImp;
    NavigationInfo navigationInfo;

    public FolderHoverView(Context context, FileRecord record, AbsMyFilesFragment fragment) {
        super(context, record, fragment);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.hover_folder_view;
    }

    @Override
    protected void initView() {
        GridView gridView = (GridView) mHoverView.findViewById(R.id.airview_preview_folder);
        navigationInfo = NavigationInfo.getInstance(NavigationMode.Normal, mRecord);

        Cursor ret = getHoverCursor();
        if (ret == null) {
            return;
        }
        if (ret.getCount() == 0) {
            ret.close();
            return;
        }
        gridView.setVisibility(View.VISIBLE);
        if (UiUtils.isInRTLMode(mContext)) {
            gridView.setPadding(mContext.getResources().getDimensionPixelSize(R.dimen.airview_preview_rtl_start_padding),
                    gridView.getPaddingTop(), gridView.getPaddingRight(), gridView.getPaddingBottom());
        }
        gridView.setLayoutParams(calculateGridViewSize(gridView, ret.getCount()));
        mAdapterImp = getHoverAdapter(navigationInfo, ret);

        gridView.setSelected(false);
        gridView.setFocusable(false);
        gridView.setAdapter(mAdapterImp);
        gridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                FileRecord record = mAdapterImp.getFileRecord(position);
                MyFilesFacade.executeRecord(mFragment.getProcessId(), mFragment.getActivity(), record);
            }
        });
    }

    protected AbsFileListAdapterImp getHoverAdapter(NavigationInfo navigationInfo, Cursor ret) {
        return new LocalHoverFileListAdapterImp(mContext, navigationInfo, R.layout.airview_preview_list_item_grid_common, ret, 0);
    }

    protected Cursor getHoverCursor() {
        Cursor ret = null;
        try {
            ret = mContext.getContentResolver().query(FileUtils.MEDIA_PROVIDER_URI, null, getSelection(), getSelectionArgs(), getOrderBy());
        } catch (SQLiteException e) {
            Log.e(this, "SQLiteException:" + e.toString());
        }

        return ret;
    }

    protected String getOrderBy(){
        return AbsFileListAdapterImp.getDefaultOrder(mContext, CategoryDbTableInfo.getInstance(), navigationInfo);
    }

    protected String getSelection(){
        boolean showHiddenFiles = PreferenceUtils.getShowHiddenFiles(mContext);

        String selection = "_data like ? AND _data not like ?";
        if (!showHiddenFiles) {
            selection = selection + " AND _data not like ?";
        }
        return selection;
    }

    protected String[] getSelectionArgs(){
        boolean showHiddenFiles = PreferenceUtils.getShowHiddenFiles(mContext);
        ArrayList<String> selectionArgsList = new ArrayList<>();
        selectionArgsList.add(mRecord.getFullPath() + "/%");
        selectionArgsList.add(mRecord.getFullPath() + "/%/%");

        if (!showHiddenFiles) {
            selectionArgsList.add(mRecord.getFullPath() + "/.%");
        }

        String[] selectionArgs = new String[selectionArgsList.size()];
        selectionArgs = selectionArgsList.toArray(selectionArgs);
        return selectionArgs;
    }

    @Override
    protected void onClose() {
        // TODO Auto-generated method stub

    }

    protected LayoutParams calculateGridViewSize(GridView gridView, int count) {
        LayoutParams params = gridView.getLayoutParams();
        Resources resources = mContext.getResources();
        int columnWidth = resources.getDimensionPixelSize(R.dimen.airview_preview_list_item_grid_width);
        int columnHeight = resources.getDimensionPixelSize(R.dimen.airview_preview_list_item_grid_height);
        int spaceSize = resources.getDimensionPixelSize(R.dimen.airview_preview_list_item_grid_spacing);

        if (count == 1) {
            params.width = columnWidth;
        } else if (count == 2) {
            params.width = columnWidth * 2 + spaceSize;
        } else {
            params.width = columnWidth * 3 + spaceSize * 2;
        }
        if (UiUtils.isInRTLMode(mContext)) {
            params.width += mContext.getResources().getDimensionPixelSize(R.dimen.airview_preview_rtl_start_padding);
        }

        if (count > 6) {
            params.height = columnHeight * 3 + spaceSize * 2;
        } else if (count > 3) {
            params.height = columnHeight * 2 + spaceSize;
        } else {
            params.height = columnHeight;
        }

        return params;
    }
}
